﻿
#include "NetworkClient.h"

ResultCode NetworkClient::ScanSubNet() {

	//释放内存空间  
	if (m_pIpAdapterInfo == nullptr)
		GetNetCardInfo();

	return ResultCode::OK;
}

void NetworkClient::GetNetCardInfo(){

	LogRecord::WriteLog(L"%s", L"GetStart");
	PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
	unsigned long  stSize = sizeof(IP_ADAPTER_INFO);
	
	int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
	

	int netCardNum = 0;
	int IPnumPerNetCard = 0;
	if (ERROR_BUFFER_OVERFLOW == nRel)
	{
		delete pIpAdapterInfo;
		pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
		nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
	}

	if (ERROR_SUCCESS == nRel)
	{

		while (pIpAdapterInfo)
		{

			cout << "NetCard Count: " << ++netCardNum << endl;
			cout << "Name: " << pIpAdapterInfo->AdapterName << endl;
			cout << "Description: " << pIpAdapterInfo->Description << endl;
			switch (pIpAdapterInfo->Type)
			{
			case MIB_IF_TYPE_OTHER:
				cout << "Type:" << "OTHER" << endl;
				break;
			case MIB_IF_TYPE_ETHERNET:
				cout << "Type:" << "ETHERNET" << endl;
				break;
			case MIB_IF_TYPE_TOKENRING:
				cout << "Type:" << "TOKENRING" << endl;
				break;
			case MIB_IF_TYPE_FDDI:
				cout << "Type:" << "FDDI" << endl;
				break;
			case MIB_IF_TYPE_PPP:
				printf("PP\n");
				cout << "Type:" << "PPP" << endl;
				break;
			case MIB_IF_TYPE_LOOPBACK:
				cout << "Type:" << "LOOPBACK" << endl;
				break;
			case MIB_IF_TYPE_SLIP:
				cout << "Type:" << "SLIP" << endl;
				break;
			default:

				break;
			}
			cout << "MAC addr: ";
			for (DWORD i = 0; i < pIpAdapterInfo->AddressLength; i++)
				if (i < pIpAdapterInfo->AddressLength - 1)
				{
					printf("%02X-", pIpAdapterInfo->Address[i]);
				}
				else
				{
					printf("%02X\n", pIpAdapterInfo->Address[i]);
				}
			cout << "IP addr: " << endl;
			//可能网卡有多IP,因此通过循环去判断  
			IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);
			do
			{
				cout << "ip address count: " << ++IPnumPerNetCard << endl;
				cout << "IP: " << pIpAddrString->IpAddress.String << endl;
				cout << "NetMask: " << pIpAddrString->IpMask.String << endl;
				cout << "GateWay: " << pIpAdapterInfo->GatewayList.IpAddress.String << endl;
				pIpAddrString = pIpAddrString->Next;
			} while (pIpAddrString);
			pIpAdapterInfo = pIpAdapterInfo->Next;
			cout << "--------------------------------------------------------------------" << endl;
		}
	}

}
